# 步骤 1: 定义中介者接口
class Mediator:
    def send(self, message, colleague):
        pass


# 步骤 2: 创建具体中介者
class ChatRoom(Mediator):
    def __init__(self):
        self.colleagues = []

    def add_colleague(self, colleague):
        if colleague not in self.colleagues:
            self.colleagues.append(colleague)

    def send(self, message, colleague):
        for coll in self.colleagues:
            if coll != colleague:
                coll.receive(message)


# 步骤 3: 定义同事类
class Colleague:
    def __init__(self, name, mediator):
        self.name = name
        self.mediator = mediator

    def send(self, message):
        print(f"{self.name} sends: {message}")
        self.mediator.send(message, self)

    def receive(self, message):
        print(f"{self.name} received: {message}")


if __name__ == '__main__':
    # 步骤 4: 使用中介者模式
    # 创建中介者
    mediator = ChatRoom()

    # 创建同事
    john = Colleague("John", mediator)
    jane = Colleague("Jane", mediator)
    dgw = Colleague("Dgw", mediator)

    # 注册同事
    mediator.add_colleague(john)
    mediator.add_colleague(jane)
    mediator.add_colleague(dgw)

    # 发送消息
    john.send("Hi there!")
    jane.send("Hey!")

'''
John sends: Hi there!
Jane received: Hi there!
Dgw received: Hi there!
Jane sends: Hey!
John received: Hey!
Dgw received: Hey!
'''